<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>API测试</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
        }
        .test-section {
            margin: 20px 0;
            padding: 15px;
            border: 1px solid #ddd;
            border-radius: 5px;
        }
        .result {
            background: #f5f5f5;
            padding: 10px;
            border-radius: 3px;
            margin-top: 10px;
            white-space: pre-wrap;
        }
        .error {
            background: #ffebee;
            color: #c62828;
        }
        .success {
            background: #e8f5e8;
            color: #2e7d32;
        }
        button {
            background: #2196f3;
            color: white;
            border: none;
            padding: 10px 20px;
            border-radius: 3px;
            cursor: pointer;
            margin: 5px;
        }
        button:hover {
            background: #1976d2;
        }
    </style>
</head>
<body>
    <h1>气象站API测试</h1>
    
    <div class="test-section">
        <h3>测试后端连接</h3>
        <button onclick="testBackendConnection()">测试连接</button>
        <div id="connection-result" class="result"></div>
    </div>
    
    <div class="test-section">
        <h3>获取最新传感器数据</h3>
        <button onclick="getLatestData()">获取最新数据</button>
        <div id="latest-data-result" class="result"></div>
    </div>
    
    <div class="test-section">
        <h3>获取历史数据</h3>
<<<<<<< HEAD
        <button onclick="getHistoryData()">获取24小时数据</button>
=======
        <button onclick="getHistoricalData()">获取24小时数据</button>
>>>>>>> 8812a9148e94551207e748afbba84869577bb3db
        <div id="history-data-result" class="result"></div>
    </div>
    
    <div class="test-section">
        <h3>模拟数据（如果后端不可用）</h3>
        <button onclick="getMockData()">获取模拟数据</button>
        <div id="mock-data-result" class="result"></div>
    </div>

    <script>
        const API_BASE = '/api';
        
        async function testBackendConnection() {
            const resultDiv = document.getElementById('connection-result');
            resultDiv.textContent = '测试中...';
            resultDiv.className = 'result';
            
            try {
                const response = await fetch(`${API_BASE}/sensors/latest`);
                if (response.ok) {
                    resultDiv.textContent = '✅ 后端连接成功！';
                    resultDiv.className = 'result success';
                } else {
                    resultDiv.textContent = `❌ 后端连接失败: ${response.status} ${response.statusText}`;
                    resultDiv.className = 'result error';
                }
            } catch (error) {
                resultDiv.textContent = `❌ 连接错误: ${error.message}`;
                resultDiv.className = 'result error';
            }
        }
        
        async function getLatestData() {
            const resultDiv = document.getElementById('latest-data-result');
            resultDiv.textContent = '获取中...';
            resultDiv.className = 'result';
            
            try {
                const response = await fetch(`${API_BASE}/sensors/latest`);
                if (response.ok) {
                    const data = await response.json();
                    resultDiv.textContent = JSON.stringify(data, null, 2);
                    resultDiv.className = 'result success';
                } else {
                    resultDiv.textContent = `获取失败: ${response.status} ${response.statusText}`;
                    resultDiv.className = 'result error';
                }
            } catch (error) {
                resultDiv.textContent = `请求错误: ${error.message}`;
                resultDiv.className = 'result error';
            }
        }
        
        async function getHistoricalData() {
            const resultDiv = document.getElementById('history-data-result');
            resultDiv.textContent = '获取中...';
            resultDiv.className = 'result';
            
            try {
                const response = await fetch(`${API_BASE}/sensors/history?hours=24`);
                if (response.ok) {
                    const data = await response.json();
                    resultDiv.textContent = `获取到 ${data.count || 0} 条历史数据\n\n${JSON.stringify(data, null, 2)}`;
                    resultDiv.className = 'result success';
                } else {
                    resultDiv.textContent = `获取失败: ${response.status} ${response.statusText}`;
                    resultDiv.className = 'result error';
                }
            } catch (error) {
                resultDiv.textContent = `请求错误: ${error.message}`;
                resultDiv.className = 'result error';
            }
        }
        
        function getMockData() {
            const resultDiv = document.getElementById('mock-data-result');
            
            // 生成模拟数据
            const mockData = {
                success: true,
                data: {
                    id: 1,
                    temperature: 25.5,
                    humidity: 65.2,
                    pressure: 101325.0,
                    lightIntensity: 850,
                    createdAt: new Date().toISOString(),
                    updatedAt: new Date().toISOString()
                },
                message: "模拟数据"
            };
            
            resultDiv.textContent = JSON.stringify(mockData, null, 2);
            resultDiv.className = 'result success';
        }
        
        // 页面加载时自动测试连接
        window.onload = function() {
            testBackendConnection();
        };
    </script>
</body>
</html> 